package cn.bdqn.t354.service.impl;

import cn.bdqn.t354.mapper.ChanceMapper;
import cn.bdqn.t354.pojo.Chance;
import cn.bdqn.t354.pojo.Customer;
import cn.bdqn.t354.pojo.User;
import cn.bdqn.t354.service.ChanceService;
import cn.bdqn.t354.service.CustomerService;
import cn.bdqn.t354.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ChanceServiceImpl implements ChanceService {
    @Resource
    private ChanceMapper chanceMapper;
    @Resource
    private DictService dictService;
    @Resource
    private CustomerService customerService;

    @Override
    public IPage<Chance> getChancesByPage(String name, String title, Integer pageNo, Integer pageSize) {
        //组装分页条件
        Page<Chance> page=new Page<>(pageNo,pageSize);
        //组装过滤条件 动态sql
        QueryWrapper<Chance> qw=new QueryWrapper();
        qw.eq("chc_status","1");
        qw.like(name!=null&&!"".equals(name),"chc_cust_name",name);
        qw.like(title!=null&&!"".equals(title),"chc_title",title);
        //只查自己创建的数据
        User user=(User)SecurityUtils.getSubject().getPrincipal();
        qw.eq("chc_create_id",user.getUsrId());
        //调用分页方法
        IPage<Chance> chancePage=chanceMapper.selectPage(page, qw);
        //返回分页对象
        return chancePage;
    }

    @Override
    public void addChance(Chance chance) {
        chanceMapper.insert(chance);
    }

    @Override
    public Chance getChance(Long id) {
        return chanceMapper.selectById(id);
    }

    @Override
    public void updateChance(Chance chance) {
        chanceMapper.updateById(chance);
    }

    @Override
    public void deleteChance(Long id) {
        chanceMapper.deleteById(id);
    }

    @Override
    public IPage<Chance> getChancesByPage(String name,String linkMan,String title,
                                          Integer pageNo, Integer pageSize) {
        //组装分页条件
        Page<Chance> page=new Page<>(pageNo,pageSize);
        //组装过滤条件 动态sql
        QueryWrapper<Chance> qw=new QueryWrapper();

        qw.in("chc_status","2","3","4");

        qw.like(name!=null&&!"".equals(name),"chc_cust_name",name);
        qw.like(linkMan!=null&&!"".equals(linkMan),"chc_linkman",linkMan);
        qw.like(title!=null&&!"".equals(title),"chc_title",title);

        User user=(User)SecurityUtils.getSubject().getPrincipal();
        if(user.getRole().getRoleId()==7){//销售主管
            //只查自己创建的数据
            qw.eq("chc_create_id",user.getUsrId());
        }else if(user.getRole().getRoleId()==2){//客户经理
            //只查指派给自己的数据
            qw.eq("chc_due_id",user.getUsrId());
        }

        //调用分页方法
        IPage<Chance> chancePage=chanceMapper.selectPage(page, qw);
        //返回分页对象
        return chancePage;
    }

    @Override
    public void successChance(Long id) {
        //1.修改销售机会状态
        //根据销售机会编号，查询销售机会信息
        Chance chance=chanceMapper.selectById(id);
        chance.setChcStatus("3");
        this.updateChance(chance);

        //2.新增客户
        Customer customer=new Customer();
        //客户编号
        int rand= (int)(Math.random()*(1000-100)+100);
        SimpleDateFormat df=new SimpleDateFormat("yyMMddHHmmss");
        customer.setCustNo("KH"+df.format(new Date())+rand);

        customer.setCustName(chance.getChcCustName());
        customer.setCustRegion(chance.getChcCustRegion());
        customer.setCustManagerId(chance.getChcDueId());
        customer.setCustManagerName(chance.getChcDueTo());
        customer.setCustLinkman(chance.getChcLinkman());
        customer.setCustLevel(Integer.valueOf(chance.getChcCustLevel()));
//        Dict dict=dictService
//                .getDictTypeAndValue("客户等级",chance.getChcCustLevel());
//        customer.setCustLevelLabel(dict.getDictItem());
        customer.setCustSatisfy(5);
        customer.setCustCredit(5);
        customer.setCustTel(chance.getChcTel());
        customer.setCustStatus("1");
        //新增客户
        customerService.addCustomer(customer);
    }

    @Override
    public void failChance(Long id) {
        //根据销售机会编号，查询销售机会信息
        Chance chance=chanceMapper.selectById(id);
        //修改销售机会的状态
        chance.setChcStatus("4");
        //修改销售机会状态
        this.updateChance(chance);
    }

}
